﻿package mortal.game.view.forging.view
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.renderer.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class BackPackView extends GSprite
    {
        private var _tabBar:GTabarNew;
        private var _pageTabData:Array;
        private var _itemList:GTileList;

        public function BackPackView()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.initTabData();
            this._tabBar = UIFactory.gTabBarNew(0, 0, this._pageTabData, 271 - 6, 497 - 65, 62, 22, this, this.onTabBarChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this.pushUIToDisposeVec(UIFactory.bg(0, 22, 265, 371 + 40, this, ImagesConst.PanelBg));
            this._itemList = UIFactory.tileList(20, 29, 250, 360 + 45, this);
            this._itemList.columnWidth = 45;
            this._itemList.rowHeight = 44;
            this._itemList.horizontalGap = 0;
            this._itemList.verticalGap = 0;
            this._itemList.setStyle("skin", new Bitmap());
            this._itemList.setStyle("cellRenderer", EquipDecomposePackCellRenderer);
            this._itemList.configEventListener(MouseEvent.CLICK, this.onListClickHandler);
            this.initItemList();
            this.addListeners();
            return;
        }// end function

        private function initTabData() : void
        {
            this._pageTabData = Language.getArray(40104);
            return;
        }// end function

        private function initItemList() : void
        {
            var _loc_1:* = Cache.instance.pack.backPackCache.getDecomposableItems();
            this.updateItemList(_loc_1);
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackItemChangeHandler);
            Dispatcher.addEventListener(EventName.ForgingOnAutoSel, this.onAutoSelHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.onPackItemChangeHandler);
            Dispatcher.removeEventListener(EventName.ForgingOnAutoSel, this.onAutoSelHandler);
            return;
        }// end function

        private function updateItemList(param1:Array) : void
        {
            var _loc_2:DataProvider = null;
            var _loc_3:ItemData = null;
            var _loc_4:DecomposeBackPackData = null;
            var _loc_5:int = 0;
            if (param1)
            {
                _loc_2 = new DataProvider();
                for each (_loc_3 in param1)
                {
                    
                    _loc_4 = new DecomposeBackPackData();
                    _loc_4.itemData = _loc_3;
                    if (Cache.instance.forging.hasDecomposeItem(_loc_3))
                    {
                        _loc_4.isDisabled = true;
                    }
                    _loc_4.isBind = ItemsUtil.isBind(_loc_3);
                    if (_loc_3 && (ItemsUtil.isEquip(_loc_3) || ItemsUtil.isOtherEquip(_loc_3)))
                    {
                        _loc_4.isNeedShowStrenLv = true;
                    }
                    _loc_2.addItem(_loc_4);
                }
                if (param1.length < 50)
                {
                    _loc_5 = param1.length;
                    while (_loc_5 < 45)
                    {
                        
                        _loc_2.addItem({});
                        _loc_5++;
                    }
                }
                this._itemList.dataProvider = _loc_2;
                this._itemList.drawNow();
            }
            return;
        }// end function

        private function onTabBarChange(event:MuiEvent = null) : void
        {
            var _loc_2:Array = null;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems();
                    break;
                }
                case 1:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems(EProp._EPropEquip);
                    break;
                }
                case 2:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems(EStuff._EStuffJewel);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.updateItemList(_loc_2);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingUpdateBtnState, this._tabBar.selectedIndex));
            return;
        }// end function

        private function onPackItemChangeHandler(param1:Object) : void
        {
            this.onTabBarChange();
            return;
        }// end function

        private function onListClickHandler(event:MouseEvent) : void
        {
            var _loc_4:Point = null;
            var _loc_5:Array = null;
            var _loc_6:Point = null;
            if (Cache.instance.forging.isDecomposeItemFull())
            {
                MsgManager.showRollTipsMsg(Language.getString(41073));
                return;
            }
            var _loc_2:* = this._itemList.selectedItem as DecomposeBackPackData;
            var _loc_3:* = this._itemList.itemToCellRenderer(this._itemList.selectedItem) as EquipDecomposePackCellRenderer;
            if (_loc_2)
            {
                if (!Cache.instance.forging.hasDecomposeItem(_loc_2.itemData))
                {
                    if (_loc_3)
                    {
                        _loc_6 = this._itemList.localToGlobal(new Point(_loc_3.x, _loc_3.y));
                    }
                    _loc_4 = (GameController.forging.view as ForgingModule).localToGlobal(new Point(228, 88));
                    _loc_5 = [_loc_2.itemData, _loc_6, _loc_4, 0.7, 0.7, this.addToDecomposePack, null];
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_5));
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingAddDecomposeItem, _loc_2.itemData));
                }
            }
            return;
        }// end function

        private function addToDecomposePack(param1:Bitmap) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingAddToDecomposePack));
            return;
        }// end function

        private function onBtnClickHandler(event:MouseEvent = null) : void
        {
            var _loc_2:Array = null;
            var _loc_5:ItemData = null;
            Cache.instance.forging.clearDecomposeItems();
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems().concat();
                    break;
                }
                case 1:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems(EProp._EPropEquip).concat();
                    break;
                }
                case 2:
                {
                    _loc_2 = Cache.instance.pack.backPackCache.getDecomposableItems(EStuff._EStuffJewel).concat();
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (_loc_2.length == 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(41074));
            }
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_5 = _loc_2[_loc_3] as ItemData;
                if (_loc_5.itemInfo.category == EStuff._EStuffJewel || ItemsUtil.isEmbedGem(_loc_5) || EquipmentUtil.isStrengthened(_loc_5) || _loc_5.itemInfo.color >= ForgingConst.EquipColor_Zi || ItemsUtil.isPetEgg(_loc_5.itemInfo) || ItemsUtil.isOtherEquip(_loc_5) || ItemsUtil.isOtherSkillBook(_loc_5) || ItemsUtil.isPetSkillBook(_loc_5) || ItemsUtil.isGoldEquipScroll(_loc_5) || ItemsUtil.isRune(_loc_5.itemInfo))
                {
                    _loc_2.splice(_loc_3, 1);
                    _loc_3 = _loc_3 - 1;
                }
                _loc_3++;
            }
            var _loc_4:Array = [];
            _loc_4 = _loc_2;
            _loc_4 = _loc_4.slice(0, ForgingConst.MaxDecomposeItemNum);
            for each (_loc_5 in _loc_4)
            {
                
                Cache.instance.forging.addDecomposeItem(_loc_5);
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingAutoSelDecomposeItem));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingAddToDecomposePack));
            return;
        }// end function

        private function onAutoSelHandler(event:DataEvent) : void
        {
            this.onBtnClickHandler();
            return;
        }// end function

        private function set isAutoSelUsable(param1:Boolean) : void
        {
            if (param1)
            {
            }
            return;
        }// end function

        private function getCurrTabItems() : Array
        {
            var _loc_1:Array = null;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_1 = Cache.instance.pack.backPackCache.getDecomposableItems();
                    break;
                }
                case 1:
                {
                    _loc_1 = Cache.instance.pack.backPackCache.getDecomposableItems(EProp._EPropEquip);
                    break;
                }
                case 2:
                {
                    _loc_1 = Cache.instance.pack.backPackCache.getDecomposableItems(EStuff._EStuffJewel);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._tabBar.dispose(param1);
            this._itemList.dispose(param1);
            this._tabBar = null;
            this._itemList = null;
            this._pageTabData = null;
            this.removeListeners();
            return;
        }// end function

    }
}
